#pragma once
#define _CRT_SECURE_NO_WANINGS 1
#pragma warning(disable: 4996)
#include<iostream>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
namespace yjl

{


    template <class T, class Container = vector<T>, class Compare = less<T> >

    class priority_queue

    {

    public:
        priority_queue()
        {}
        template <class InputIterator>
        
        priority_queue(InputIterator first, InputIterator last)
        {
            InputIterator cur = first;
            while (cur != last)
            {
                c.push(*cur);
                cur++;
            }
            for (int i = ((c.size()) - 1 - 1) / 2; i >= 0; i--)
            {
                adjustdown(i);
            }
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        T& top() const
        {
            return c[0];
        }
        
        void push(const T& x)
        {
            if (c.empty())
            {
                c.push_back(x);
            }
            else
            {
                c.push_back(x);
                adjustup(c.size() - 1);
            }
        }

        void pop()
        {
            assert(!c.empty());
            swap(c[0], c[c.size()]);
            c.pop_back();
        }

    private:
        void adjustup( size_t child)
        {
            
            size_t parent = (child - 1) / 2;
            while (child > 0)
            {
                if (comp(c[parent], c[child]))
                {
                    swap(c[child], c[parent]);
                    child = parent;
                    parent = (parent - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }
        void adjustdown(size_t parent)
        {
            size_t child = parent * 2 + 1;
            
            while (parent >= 0)
            {
                if (child + 1 != c.size() && comp(c[child], c[child + 1]))
                {
                    child = child + 1;
                }
                if (comp(c[parent], c[child]))
                {
                    swap(c[parent], c[child]);
                    child = parent;
                    parent = (parent - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }
        Container c;

        Compare comp;

    };

};